<?php
class Product_model extends CI_Model
{
	public $tbl = 'product';

	public function __contruct()
	{
		parent::__construct();	
	}

	public function All($warehouseId)
	{
		$queryString = "SELECT tblProduct.*, provider.Name AS ProviderName, tblStockReceiptProduct.*
						FROM
						(SELECT * FROM product WHERE warehouseId = $warehouseId) AS tblProduct
					    JOIN
					    (SELECT *, sum(Mount) AS total_recept FROM stock_receipt_product GROUP BY ProductId) AS tblStockReceiptProduct
					    ON tblProduct.Id = tblStockReceiptProduct.ProductId
					    JOIN provider ON tblProduct.ProviderId = provider.Id";

		$query = $this->db->query($queryString);
		
		return $query->result();
	}

	public function Add($warehouseId)
	{
		$data = array(
			'Name' => $this->input->post('Name'),
			'UnitId' => $this->input->post('UnitId'),
			'ProviderId' => $this->input->post('ProviderId'),
			'WarehouseId' => $warehouseId
			);

		return $this->db->insert($this->tbl, $data);
	}	
}
?>